package com.whli.autumn.mall.web.controller.metadata;

import com.github.pagehelper.PageInfo;
import com.whli.autumn.core.app.annotation.ApiVersion;
import com.whli.autumn.core.extend.controller.BaseController;
import com.whli.autumn.core.permission.annotation.RequirePermissions;
import com.whli.autumn.core.share.ResponseResult;
import com.whli.autumn.mall.web.controller.convert.metadata.ProductInfoConvert;
import com.whli.autumn.mall.metadata.domain.dto.productinfo.ProductInfoAddDTO;
import com.whli.autumn.mall.metadata.domain.dto.productinfo.ProductInfoPageDTO;
import com.whli.autumn.mall.metadata.domain.dto.productinfo.ProductInfoStatusDTO;
import com.whli.autumn.mall.metadata.domain.dto.productinfo.ProductInfoUpdateDTO;
import com.whli.autumn.mall.metadata.domain.entity.ProductInfo;
import com.whli.autumn.mall.metadata.service.IProductInfoService;
import com.whli.autumn.mall.metadata.domain.vo.productinfo.ProductInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>商品信息API</p>
 *
 * @author whli
 * @version 1.0.0
 * @since 2023-04-09 09:32
 */
@Api(tags = "元数据管理-商品信息")
@ApiVersion
@RestController
@RequestMapping(value = "/metadata/productInfo")
public class ProductInfoController extends BaseController<ProductInfo, IProductInfoService> {

    /**
     * <p>查询商品信息详情</p>
     *
     * @author whli
     * @since 2023-04-09 09:32
     * @param id 商品信息主键
     * @return
     */
    @ApiOperation(value = "查询商品信息", httpMethod = "GET")
    @ApiImplicitParam(value = "主键", name = "id", required = true)
    @RequirePermissions("metadata:product-info:view")
    @GetMapping
    public ResponseResult<ProductInfoVO> view(String id) {
        ProductInfo entity = service.getByPK(id);
        return ResponseResult.success(ProductInfoConvert.INSTANCE.domainToVO(entity));
    }

    /**
     * <p>分页查询商品信息</p>
     *
     * @author  whli
     * @since   2023-04-09 09:32
     * @param   entity 分页参数
     * @return
     */
    @ApiOperation(value = "分页查询商品信息", httpMethod = "GET")
    @RequirePermissions("metadata:product-info:list")
    @GetMapping("page")
    public ResponseResult<PageInfo<ProductInfoVO>> listByPage(ProductInfoPageDTO entity) {
        PageInfo<ProductInfoVO> page = service.listByPage(entity);
        return ResponseResult.success(page);
    }

    /**
     * <p>新增商品信息</p>
     *
     * @author  whli
     * @since   2023-04-09 09:32
     * @param   entity 新增参数
     * @return
     */
    @ApiOperation(value = "新增商品信息", httpMethod = "POST", consumes = "application/json")
    @RequirePermissions("metadata:product-info:add")
    @PostMapping
    public ResponseResult<String> save(@RequestBody @Valid ProductInfoAddDTO entity) {
        ProductInfo productInfo = ProductInfoConvert.INSTANCE.addToDomain(entity);
        String id = service.save(productInfo);
        return !StringUtils.isEmpty(id) ? ResponseResult.success("新增商品信息成功", id) : ResponseResult.fail("新增商品信息失败");
    }

    /**
     * <p>修改商品信息</p>
     *
     * @author  whli
     * @since   2023-04-09 09:32
     * @param   entity  修改参数
     * @return
     */
    @ApiOperation(value = "修改商品信息", httpMethod = "PUT", consumes = "application/json")
    @RequirePermissions("metadata:product-info:update")
    @PutMapping
    public ResponseResult<String> update(@RequestBody @Valid ProductInfoUpdateDTO entity) {
        ProductInfo productInfo = ProductInfoConvert.INSTANCE.updateToDomain(entity);
        String id = service.update(productInfo);
        return !StringUtils.isEmpty(id) ? ResponseResult.success("修改商品信息成功") : ResponseResult.fail("修改商品信息失败");
    }

    /**
     * <p>删除商品信息</p>
     *
     * @author  whli
     * @since   2023-04-09 09:32
     * @param   ids 主键集合 java.util.List<String>
     * @return
     */
    @ApiOperation(value = "删除商品信息", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "主键集合", name = "ids", required = true)
    })
    @RequirePermissions("metadata:product-info:delete")
    @DeleteMapping
    public ResponseResult<String> delete(@RequestBody List<String> ids) {
        boolean flag = service.deleteBatch(ids);
        return flag ? ResponseResult.success("删除商品信息成功") : ResponseResult.fail("删除商品信息失败");
    }

    /**
     * <p>启用商品信息</p>
     *
     * @author whli
     * @since 2023/4/29 14:36
     * @param entity
     * @return
     */
    @ApiOperation(value = "启用商品信息", httpMethod = "PUT")
    @RequirePermissions({"metadata:product-info:enable"})
    @PutMapping({"enable"})
    public ResponseResult<String> enable(@RequestBody @Valid ProductInfoStatusDTO entity) {
        boolean flag = service.enable(entity.getId());
        return flag ? ResponseResult.success("启用商品信息成功") : ResponseResult.fail("启用商品信息失败");
    }

    /**
     * <p>禁用商品信息</p>
     *
     * @author whli
     * @since 2023/4/29 14:36
     * @param entity
     * @return
     */
    @ApiOperation(value = "禁用商品信息", httpMethod = "PUT")
    @RequirePermissions({"metadata:product-info:disable"})
    @PutMapping({"disable"})
    public ResponseResult<String> disable(@RequestBody @Valid ProductInfoStatusDTO entity) {
        boolean flag = service.disable(entity.getId());
        return flag ? ResponseResult.success("禁用商品信息成功") : ResponseResult.fail("禁用商品信息失败");
    }

}
